/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package telas;

import Thread.ContaTempo;
import controle.ControleTempo;
import classes.Clientes;
import classes.Dados;
import controle.HorarioAtual;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;

/**
 *
 * @author pc
 */
public class LiberaComputador extends javax.swing.JDialog {

    Clientes cliente;
    Dados dados;
    ControleTempo controle;
    HorarioAtual horaAtual;

    public LiberaComputador(java.awt.Frame parent, boolean modal, Clientes cliente, ControleTempo controle, int pos) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
        jTTempo.setFocusable(true);
        this.cliente = cliente;
        this.controle = controle;
        
        dados = cliente.getCliente(pos);
        horaAtual = new HorarioAtual();
        
        iniciaTela();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPDados = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTCpf = new javax.swing.JFormattedTextField();
        jTTelefone = new javax.swing.JFormattedTextField();
        jTNome = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jTEntrada = new javax.swing.JFormattedTextField();
        jTTempo = new javax.swing.JFormattedTextField();
        jTSaida = new javax.swing.JFormattedTextField();
        jBLiberar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Eclipse Lan House");

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("Nome:");

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("CPF:");

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Telefone:");

        try {
            jTCpf.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        javax.swing.GroupLayout jPDadosLayout = new javax.swing.GroupLayout(jPDados);
        jPDados.setLayout(jPDadosLayout);
        jPDadosLayout.setHorizontalGroup(
            jPDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPDadosLayout.createSequentialGroup()
                .addGroup(jPDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPDadosLayout.createSequentialGroup()
                        .addGap(57, 57, 57)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTNome, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPDadosLayout.createSequentialGroup()
                        .addGap(57, 57, 57)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTCpf, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPDadosLayout.createSequentialGroup()
                        .addGap(61, 61, 61)
                        .addComponent(jLabel3)
                        .addGap(4, 4, 4)
                        .addComponent(jTTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPDadosLayout.setVerticalGroup(
            jPDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPDadosLayout.createSequentialGroup()
                .addContainerGap(19, Short.MAX_VALUE)
                .addGroup(jPDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(10, 10, 10)
                .addGroup(jPDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(10, 10, 10)
                .addGroup(jPDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addContainerGap())
        );

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel5.setText("Horário Inicial:");

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel6.setText("Tempo:");

        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel7.setText("Horário Final:");

        try {
            jTEntrada.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##:##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jTEntrada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTEntradaActionPerformed(evt);
            }
        });

        try {
            jTTempo.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##:##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jTTempo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTTempoActionPerformed(evt);
            }
        });
        jTTempo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTTempoKeyReleased(evt);
            }
        });

        try {
            jTSaida.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##:##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(123, 123, 123)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jTTempo, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTEntrada, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTSaida, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(133, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(15, 15, 15)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jTTempo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(15, 15, 15)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jTSaida, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(12, Short.MAX_VALUE))
        );

        jBLiberar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/liberar.png"))); // NOI18N
        jBLiberar.setText("Liberar");
        jBLiberar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBLiberarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(182, 182, 182)
                        .addComponent(jBLiberar)))
                .addGap(7, 10, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPDados, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jPDados, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addComponent(jBLiberar)
                .addContainerGap(12, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBLiberarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBLiberarActionPerformed
        
        if(jTTempo.getText().replace(":", "").trim().isEmpty()){
            JOptionPane.showMessageDialog(this, "Preencha o Tempo","Aviso",JOptionPane.INFORMATION_MESSAGE);
            
        }else{
           
            int tempo = Integer.parseInt(jTTempo.getText().replace(":", "").trim());
            
            dados = new Dados(jTNome.getText(), 
                            jTCpf.getText(), 
                            jTTelefone.getText(), 
                            jTEntrada.getText(), 
                            jTTempo.getText(), 
                            jTSaida.getText());
            int cadastrou = controle.liberaPc(dados);
            if(cadastrou == 1){
                tempo = tempo  * 60 * 1000;
                new ContaTempo(dados,(long) tempo,controle).start();
                JOptionPane.showMessageDialog(this, "Liberado com Sucesso","Aviso",JOptionPane.INFORMATION_MESSAGE);
                this.dispose();
                new ConsultaLiberados(null, true, controle).setVisible(true);
                
                
            }else if(cadastrou == 0){
                JOptionPane.showMessageDialog(this, "O cliente ja esta utilizando o sistema","Aviso",JOptionPane.INFORMATION_MESSAGE);
            }else if(cadastrou == -1){
                JOptionPane.showMessageDialog(this, "Limite de Computadores atingido","Aviso",JOptionPane.INFORMATION_MESSAGE);
            }
        }
    }//GEN-LAST:event_jBLiberarActionPerformed

    private void jTTempoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTTempoKeyReleased
        if(jTTempo.getText().trim().toCharArray().length == 5){
            String entrada[] = jTEntrada.getText().split(":");
            String tempo[] = jTTempo.getText().split(":");           
            
            
             DecimalFormat df = new DecimalFormat("00");
             int saidaHora = Integer.parseInt(entrada[0]) + Integer.parseInt(tempo[0]);
             int saidaMinutos = Integer.parseInt(entrada[1]) + Integer.parseInt(tempo[1]);
            
            if(saidaMinutos >=60){
                saidaHora++;
                saidaMinutos -= 60;
            }
            if(saidaHora >= 24){
                saidaHora -= 24;
            }
              jTSaida.setText(String.valueOf(df.format(saidaHora) + ":" + df.format(saidaMinutos)));  
            
        
        }
    }//GEN-LAST:event_jTTempoKeyReleased

    private void jTEntradaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTEntradaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTEntradaActionPerformed

    private void jTTempoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTTempoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTTempoActionPerformed

    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBLiberar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPDados;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JFormattedTextField jTCpf;
    private javax.swing.JFormattedTextField jTEntrada;
    private javax.swing.JTextField jTNome;
    private javax.swing.JFormattedTextField jTSaida;
    private javax.swing.JFormattedTextField jTTelefone;
    private javax.swing.JFormattedTextField jTTempo;
    // End of variables declaration//GEN-END:variables

    private void iniciaTela(){
        
        
        jTEntrada.setEditable(false);
        jTSaida.setEditable(false);
        jTEntrada.setText(horaAtual.horaLocal());
        
        jTNome.setEditable(false);
        jTCpf.setEditable(false);
        jTTelefone.setEditable(false);
        
        if(cliente.getContContatos() > 0){
            jTNome.setText(dados.getNome());
            jTCpf.setText(dados.getCpf());
            jTTelefone.setText(dados.getTelefone());
        }else{
            jBLiberar.setEnabled(false);
        }
    }
    
}
